//app.js
App({ 
  data:{
      url: 'http://192.168.1.8:1115'
    //url:'http://192.168.1.6:7070'
  },
  onLaunch: function () {   
    wx.login({
      success: function (res) {
        if (res.code) {
          var code = res.code
          wx.getUserInfo({
            success: function (res) {
              var objz = {};
              objz.logo = res.userInfo.avatarUrl;
              objz.hyname = res.userInfo.nickName;
              objz.provice = res.userInfo.province;
              objz.city = res.userInfo.city;
              wx.setStorageSync('userInfo', objz);//存储userInfo 
              try {
                wx.request({
                  url: 'http://192.168.1.11:1114/shop/Member/WXLogin',
                  data: {
                    code: code
                  },
                  success: function (res) {
                    var obj = {}; 
                    obj.openid = res.data.openid;
                    obj.expires_in = Date.now() + res.data.expires_in;
                    wx.setStorageSync('user', obj);//存储openid
                    wx.request({
                      url: 'http://192.168.1.11:1114/shop/Member/Add2',
                      data: {
                        OpenId: res.data.openid,
                        Name: wx.getStorageSync('userInfo').hyname,
                        Logo: wx.getStorageSync('userInfo').logo,
                        Address: wx.getStorageSync('userInfo').city
                      },
                      header: {
                        'content-type': 'application/json'
                      },
                      success: function (data) {
                       
                      },
                      fail: function (error) {
                        console.info(error);
                      }
                    });
                  },
                  error: function (res) {
                    wx.showToast({
                      title: `获取登录态失败`,
                      showCancel: false,
                      duration: 2000
                    })
                  }
                })
              }
              catch (e) {
                wx.showToast({
                  title: ''+e+'',
                  showCancel: false,
                  duration: 2000
                })
              }
            },
            fail: function () {
              wx.showModal({
                title: '温馨提示',
                content: '拒绝授权将不能正常使用功能，请重启小程序后重新授权',
              })
            }
          });
        }
      }
    })
    // }
  },

  getUserInfo: function(cb) {
    var that = this
    if (this.globalData.userInfo) {
      typeof cb == "function" && cb(this.globalData.userInfo)
    } else {
      //调用登录接口
      wx.getUserInfo({
        withCredentials: false,
        success: function(res) {
          that.globalData.userInfo = res.userInfo
          typeof cb == "function" && cb(that.globalData.userInfo)
        }
      })
    }
  },

  globalData: {
    userInfo: null,
  }
})
